<template>
  <van-form>
    <van-field v-model="item.value" :class="item.class" :type="item.colType" :border="true" :placeholder="placeholder" :disabled="item.disabled" :rules="[{ required: true, message: '请填写身份证号' },{validator, message: '身份证号码格式错误！'}]" @blur="onBlur">
      <template #label>
        <span class="custom-title">{{ label }}
          <van-icon v-if="help" name="question" @click="showHelp" />
        </span>
      </template>
    </van-field>
  </van-form>
</template>

<script>
import EleBase from './EleBase'

export default {
  name: 'EleIDCardNumber',
  extends: EleBase,
  props: {
    item: {
      type: Object, // 类型
      default: null // 默认值
    }
  },
  data() {
    return {
      inputModel: ''
    }
  },
  computed: {
    errMsg() {
      let str = ''
      if (this.item.required) {
        if (this.item.undermsg === '') {
          str = '请选择' + this.item.label
        } else {
          str = this.item.undermsg
        }
      }
      return str
    },
    placeholder() {
      let str = ''
      if (this.item.placeholder) {
        str = this.item.placeholder
      } else if (this.item.label) {
        str = '请输入' + this.item.label
      }
      return str
    }
  },
  mounted () {
  },
  methods: {
    validator(value) {
      debugger
      const card15 = /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$/
      const card18 = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      return card15.test(value) || card18.test(value)
    },
    onBlur() {
      const linkFields = this.item.linkFields
      const idValue = this.$checkIdCard(this.item.value)
      this.$emit('idCardNumberCallback', { field: JSON.parse(linkFields), value: idValue })
    }
  }
}
</script>
